home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_oth / minmax / minmax.pro
Text File  |  1986-07-05  |  2KB  |  68 lines

  1. /*
  2. MINIMUM/MAXIMUM OF INTEGER LISTS IN TURBO PROLOG
  3.         BY GARRY J. VASS  [72301,3311]
  4.         
  5. */
  6. nowarnings
  7.  
  8. domains
  9.     list = integer*
  10. predicates
  11.     integer_list_maximum(integer, list,integer)
  12.     integer_list_minimum(integer, list, integer) 
  13.     demonstrate                  
  14.     any_list(list)        
  15. clauses
  16.     any_list([-6,-1,-2,-88,-2,200,-3,-4,-5]).
  17.  
  18.  
  19. /*   if the list is empty, then the highest value */
  20. /*   found thus far (in "X") must be the maximum. */
  21. /*   Set "M" to the maximum because "X" will get  */
  22. /*   reassigned.                                  */
  23.  
  24.     integer_list_maximum(X, [], M) if M = X.
  25.  
  26. /*   if the head of the list is greater than  */
  27. /*   the highest value encountered so far then*/
  28. /*   compare the head to the rest of the list */
  29.  
  30.     integer_list_maximum(X, [H|T],M) if
  31.         H >= X and
  32.         integer_list_maximum(H,T,M).
  33.  
  34. /*   if the above two rules don't work, then */
  35. /*   continue the tail recursion process.    */
  36.  
  37.     integer_list_maximum(X,[H|T],M) if
  38.         integer_list_maximum(X,T,M).    
  39.  
  40.  
  41.  
  42. /*   same logic applies to min predicate  */
  43.  
  44.     integer_list_minimum(X,[],M) if M = X.        
  45.     integer_list_minimum(X, [H|T],M) if
  46.         H <= X and
  47.         integer_list_minimum(H,T,M).
  48.     integer_list_minimum(X, [H|T],M) if
  49.         integer_list_minimum(X,T,M).
  50.  
  51.     demonstrate if
  52.         any_list([H|T]) and
  53.         integer_list_maximum(H,[H|T],Maximum) and
  54.         write("Maximum = ",Maximum) and nl and
  55.         integer_list_minimum(H,[H|T],Minimum) and
  56.         write("Minimum = ",Minimum) and nl.        
  57.         
  58.     /* first variable is initialized to head   */
  59.     /* of list to avoid hard-coding an initial */
  60.     /* value.                                  */
  61. /*
  62. goal
  63.     demonstrate
  64. */        
  65.     G-²
  66. ü##á$8>┌/╛÷:╛í
  67. ü â╘à[┌    »Pi┴k▌εV%ò0ZPEεΣs|Zgrx1ìLU ╗Æ┬«▄╔0
  68. @fü¬╚c╛)N(2 ñ└Σ$Ç HYMV₧░y